package binarytree

func rob3(root *TreeNode) int {

	max := func(i, j int) int {
		if i > j {
			return i
		}
		return j
	}

	var helper func(root *TreeNode) []int
	helper = func(root *TreeNode) []int {

		if root == nil {
			return []int{0, 0}
		}

		left := helper(root.Left)
		right := helper(root.Right)

		steal := root.Val + left[0] + right[0]
		nosteal := max(left[0], left[1]) + max(right[0], right[1])

		return []int{nosteal, steal}

	}

	res := helper(root)
	return max(res[0], res[1])

}
